package com.ly.hotel.spainterface.jxd.activity;

import android.app.Fragment;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.IdRes;
import androidx.viewpager.widget.ViewPager;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.MyLogsManager;
import com.ly.hotel.msgevent.SendLogEvent;
import com.ly.hotel.spainterface.jg.view.CustomToast;
import com.ly.hotel.spainterface.jxd.adapter.MassageAdapter;
import com.ly.hotel.spainterface.jxd.adapter.PopTherapListAdapter;
import com.ly.hotel.spainterface.jxd.adapter.StatusFragmentAdapter;
import com.ly.hotel.spainterface.jxd.bean.MachineBean;
import com.ly.hotel.spainterface.jxd.bean.Massage;
import com.ly.hotel.spainterface.jxd.bean.PluginBean;
import com.ly.hotel.spainterface.jxd.bean.SortByArtNo;
import com.ly.hotel.spainterface.jxd.bean.Therapy;
import com.ly.hotel.spainterface.jxd.dialog.JxdJSPopupTherapyDetailsDialog;
import com.ly.hotel.spainterface.jxd.utils.AvoidDoubleClickedUtils;
import com.ly.hotel.spainterface.jxd.utils.MachineUtils;
import com.ly.hotel.spainterface.jxd.utils.UdpUtils;
import com.ly.hotel.spainterface.jxd.utils.XmlParseUtils;
import com.ly.hotel.util.ProcessWaitingUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by SHUBEN on 2018/3/27.
 */

public class JxdJSPopupWindow extends Service {

    // 定义浮动窗口布局
    private View mFloatLayout;
    private WindowManager.LayoutParams wmParams;
    // 创建浮动窗口设置布局参数的对象
    private WindowManager mWindowManager;
    private MyApplication app;
    private static final String TAG = "JxdJSPopupWindow";
    private Button exitBt;
    private Context context;
    private PluginBean pluginBean;
    private MachineBean machineBean;
    private ExpandableListView popItemExListView;
    private LinearLayout popItemShowMore;
    private final int GET_NO_HALL_ITEM = 0,GET_HALL_ITEM = 1,GET_THERAPY_DATA = 2,GET_THERAPY_DATA_BY_ARTNO = 8;
    private List<String> massageCategoryGroup = null;
    private List<List<Massage>> massageNamesChild= new ArrayList<>();
    private boolean isShowHallItem = false;// 是否显示大堂项目
    private boolean isShowNoHallItem = false;// 是否显示全身项目
    private List<Massage> hallMassage = new ArrayList<>();
    private List<Massage> noHallMassage = new ArrayList<>();
    private ViewPager popStatusViewPager;
    private LinearLayout popStatusTab;
    private LinearLayout potStatusShowMore;
    private TextView popFreeText;
    private TextView popWorkingText;
    private TextView popAllText;
    private View popCursorLine;
    private int statusTabWidth;// 状态栏三分之一的长度
    private int offset;// 指示条初始位置
    private RelativeLayout connectErrorTips;
    private LinearLayout popTherapyList;
    private List<Fragment> fragments;
    private StatusFragmentAdapter fragmentAdapter;
    private boolean isFirstEnter = true;
    private LinearLayout.LayoutParams llPopCursorLine;
    private GridView popStatusSelect;
    private RadioGroup statusGroup;
    private RadioButton freeSelect;
    private RadioButton workingSelect;
    private RadioButton allSelect;
    private GridView popTherapyGridView;
    private Massage massage;
    private String status = "空闲";// 默认选中状态的空闲
    private String sex= "全部";// 默认选中性别的全部
    private String massageName;
    private PopTherapListAdapter adapter;
    private List<Therapy> therapies = new ArrayList<>();
    private RelativeLayout noStatusTips;
    private JxdJSPopupTherapyDetailsDialog detailsDialog;
    private List<Therapy> therapyList;
    private RadioGroup sexGroup;
    private RadioButton sexAll;
    private RadioButton sexFemale;
    private RadioButton sexMale;

    @Override
    public void onCreate() {
        super.onCreate();
        app = (MyApplication)getApplicationContext();
        context = MyApplication.getContext();
        createFloatView();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void createFloatView() {
        wmParams = new WindowManager.LayoutParams();
        // 获取WindowManagerImpl.CompatModeWrapper
        mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);

        DisplayMetrics dm = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getMetrics(dm);

        // 设置window type
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        // 设置图片格式，效果为背景透明
        wmParams.format = PixelFormat.RGBA_8888;

        // 调整悬浮窗显示的停靠位置为左侧置顶
        wmParams.gravity = Gravity.CENTER;
        // 以屏幕左上角为原点，设置x、y初始值

        // 设置悬浮窗口长宽数据
        wmParams.width = getResources().getDimensionPixelSize(R.dimen.px1470);//WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = getResources().getDimensionPixelSize(R.dimen.px831);// WindowManager.LayoutParams.WRAP_CONTENT;

        // 获取浮动窗口视图所在布局
        LayoutInflater inflater = LayoutInflater.from(getApplication());
        mFloatLayout = inflater.inflate(R.layout.jxd_popup_jishi, null);

        // 添加mFloatLayout
        mWindowManager.addView(mFloatLayout, wmParams);
        mFloatLayout.setVisibility(View.VISIBLE);
        ProcessWaitingUtil.showProcessDialog(context,getResources().getString(R.string.loading_tips),10000);


        getMachineInfo();
        getPluginConfig();

        findView();
        setListener();
    }

    // 获得插件配置
    public void getPluginConfig(){
        pluginBean = XmlParseUtils.getInstance().parsePluginConfig(context, MyApplication.pluginCfg,MyApplication.interfaceVersion);
        if(pluginBean != null){
            String cashierIP = pluginBean.getIp();
            String cashierPORT = pluginBean.getPort();
            if(!TextUtils.isEmpty(cashierIP) && !TextUtils.isEmpty(cashierPORT)){
                massageCategoryGroup = new ArrayList<>();
                if(!TextUtils.isEmpty(pluginBean.getItemclass1())){
                    try{
                        String[] itemClass1 = pluginBean.getItemclass1().split(",");
                        if(itemClass1[1].equals("true")){
                            massageCategoryGroup.add(itemClass1[0]);
                            isShowHallItem = true;
                        }
                    }catch (Exception e){
                        //Log.i(TAG,"itemClass1配置出错！");
                    }
                }else{
                    massageCategoryGroup.add("大堂项目");
                }

                if(!TextUtils.isEmpty(pluginBean.getItemclass2())){
                    try{
                        String[] itemClass2 = pluginBean.getItemclass2().split(",");
                        if(itemClass2[1].equals("true")){
                            massageCategoryGroup.add(itemClass2[0]);
                            isShowNoHallItem = true;
                        }
                    }catch (Exception e){
                        Log.i(TAG,"itemClass2配置出错！");
                    }
                }else{
                    massageCategoryGroup.add("全身项目");
                }

                // 当itemClass1和itemClass2配置中都为false的是否，默认显示全部
                if(massageCategoryGroup.size() == 0){
                    massageCategoryGroup.add("大堂项目");
                    massageCategoryGroup.add("全身项目");
                }

                // 请求大堂项目数据
                getHallItem();
            }else{
                CustomToast customToast = CustomToast.makeText(context,"请检查后台ip或port配置是否有误！", Toast.LENGTH_SHORT,false);
                customToast.setGravity(Gravity.CENTER,0,0);
                customToast.show();
            }
        }else{
            CustomToast customToast = CustomToast.makeText(context,"请检查后台配置是否有误！", Toast.LENGTH_SHORT,false);
            customToast.setGravity(Gravity.CENTER,0,0);
            customToast.show();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    private void findView() {
        TextView title = mFloatLayout.findViewById(R.id.popup_name);
        title.setText(R.string.naviJishiName);
        popItemExListView = mFloatLayout.findViewById(R.id.pop_item_exlistview);
        popItemShowMore = mFloatLayout.findViewById(R.id.pop_item_down_more);
        exitBt = mFloatLayout.findViewById(R.id.popup_back);
        connectErrorTips = mFloatLayout.findViewById(R.id.connect_error);
        popTherapyList = mFloatLayout.findViewById(R.id.pop_therapy_list);

        statusGroup = mFloatLayout.findViewById(R.id.status_select);
        freeSelect = mFloatLayout.findViewById(R.id.free_select);
        workingSelect = mFloatLayout.findViewById(R.id.working_select);
        allSelect = mFloatLayout.findViewById(R.id.all_select);
        statusGroup.setOnCheckedChangeListener(new StatusRadioButtonListener());

        sexGroup = mFloatLayout.findViewById(R.id.sex_select);
        sexAll = mFloatLayout.findViewById(R.id.sex_all);
        sexFemale = mFloatLayout.findViewById(R.id.sex_female);
        sexMale = mFloatLayout.findViewById(R.id.sex_male);
        sexGroup.setOnCheckedChangeListener(new SexRadioButtonListener());

        popTherapyGridView = mFloatLayout.findViewById(R.id.pop_therapy_gridview);
        noStatusTips = mFloatLayout.findViewById(R.id.no_this_status_tips);
    }

    class StatusRadioButtonListener implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId){
                case R.id.free_select:
                    status = "空闲";
                    ProcessWaitingUtil.showProcessDialog(context,getResources().getString(R.string.loading_tips),10000);
                    getTherapyData(massageName);
                    break;

                case R.id.working_select:
                    status = "在做钟";
                    ProcessWaitingUtil.showProcessDialog(context,getResources().getString(R.string.loading_tips),10000);
                    getTherapyData(massageName);
                    break;

                case R.id.all_select:
                    status = "全部";
                    ProcessWaitingUtil.showProcessDialog(context,getResources().getString(R.string.loading_tips),10000);
                    getTherapyData(massageName);
                    break;

                default:
                    break;
            }
        }
    }

    class SexRadioButtonListener implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId){
                case R.id.sex_all:
                    sex = "全部";
                    ProcessWaitingUtil.showProcessDialog(context,getResources().getString(R.string.loading_tips),10000);
                    getTherapyData(massageName);
                    break;

                case R.id.sex_female:
                    sex = "女";
                    ProcessWaitingUtil.showProcessDialog(context,getResources().getString(R.string.loading_tips),10000);
                    getTherapyData(massageName);
                    break;

                case R.id.sex_male:
                    sex = "男";
                    ProcessWaitingUtil.showProcessDialog(context,getResources().getString(R.string.loading_tips),10000);
                    getTherapyData(massageName);
                    break;

                default:
                    break;
            }
        }
    }

    private void setListener() {
        exitBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopSelf();
            }
        });
    }

    // 获得电视相关信息
    public void getMachineInfo(){
        machineBean = MachineUtils.getHostAndTvInfo();
        /*if(machineBean != null){
            Log.i(TAG,"serverIP=" +  machineBean.getServerIP());
            Log.i(TAG,"serverPORT=" +  machineBean.getServerPORT());
            Log.i(TAG,"mac=" +  machineBean.getMac());
            Log.i(TAG,"categoryName=" +  machineBean.getCategoryName());
            Log.i(TAG,"tvName=" +  machineBean.getTvName());
        }*/
    }

    // 请求大堂项目数据
    public void getHallItem(){
        String req = "<gs ver=\"1.0\" function=\"桑拿做钟类型\" mode = \"request\"><MassageTypeNo>" +
                "</MassageTypeNo><MassageName></MassageName><IsHall>1</IsHall><OnlyMassageName>0</OnlyMassageName>" +
                "<SpecSetting>1</SpecSetting></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(pluginBean.getIp(),pluginBean.getPort(),req,handler,GET_HALL_ITEM);
    }

    // 请求全身项目数据
    public void getNoHallItem(){
        String req = "<gs ver=\"1.0\" function=\"桑拿做钟类型\" mode = \"request\"><MassageTypeNo>" +
                "</MassageTypeNo><MassageName></MassageName><IsHall>0</IsHall><OnlyMassageName>0</OnlyMassageName>" +
                "<SpecSetting>1</SpecSetting></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(pluginBean.getIp(),pluginBean.getPort(),req,handler,GET_NO_HALL_ITEM);
    }

    public void getTherapyData(String massageName){
        Log.i(TAG,"status=" + status);
        Log.i(TAG,"massageName=" + massageName);
        String req = "<gs ver=\"1.0\" function=\"技师资料查询\" mode=\"request\"><ArtNo></ArtNo>" +
                "<Status>" + status + "</Status><Onduty>全部</Onduty><ArtModeNo>" + massageName + "</ArtModeNo>" +
                "<Columns>artNo,downminute,status,onduty,cname,sex</Columns><ArtTypeNo></ArtTypeNo></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(pluginBean.getIp(),pluginBean.getPort(),req,handler,GET_THERAPY_DATA);
    }

    // 请求技师数据
    public void getTherapyDataByArtNo(String artNo){
        ProcessWaitingUtil.showProcessDialog(context,getResources().getString(R.string.loading_tips),10000);
        Log.i(TAG,"artNo=" + artNo);
        String req = "<gs ver=\"1.0\" function=\"技师资料查询\" mode = \"request\"><ArtNo>" + artNo + "</ArtNo><Status>" +
                "</Status><Onduty></Onduty><ArtModeNo></ArtModeNo><Columns></Columns><ArtTypeNo>" +
                "</ArtTypeNo></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(pluginBean.getIp(),pluginBean.getPort(),req,handler,GET_THERAPY_DATA_BY_ARTNO);
    }

    // 设置做钟类型Adapter
    public void setMassageAdapter(){
        if(hallMassage.size() == 0 && noHallMassage.size() == 0){
            connectErrorTips.setVisibility(View.VISIBLE);
            popTherapyList.setVisibility(View.GONE);
        }else{
            if(massageCategoryGroup.size() == massageNamesChild.size()){
                popTherapyList.setVisibility(View.VISIBLE);
                final MassageAdapter massageAdapter = new MassageAdapter(this,massageCategoryGroup,massageNamesChild);
                popItemExListView.setAdapter(massageAdapter);
                if(massageAdapter != null){// 默认打开所有组
                    for(int i = 0;i < massageCategoryGroup.size();i ++){
                        popItemExListView.expandGroup(i);
                    }
                }
                // 监听按摩项目的点击事件
                popItemExListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                        massageAdapter.setDefaultGroupPostion(groupPosition);
                        massageAdapter.setDefaultChildPosition(childPosition);
                        List<Massage> massages = massageNamesChild.get(groupPosition);
                        massage = massages.get(childPosition);
                        //Log.i(TAG,"点击的做钟项目：" + massage.getMassagename());
                        //Log.i(TAG,"是否是大堂项目：" + massage.getIshall());
                        massageName = massage.getMassagename();
                        ProcessWaitingUtil.showProcessDialog(context,getResources().getString(R.string.loading_tips),10000);
                        getTherapyData(massageName);
                        return true;
                    }
                });
                // 认点击childView的第一项
                popItemExListView.performItemClick(null,1,0);// 第三个参数为零，第二个参数为从第0个groupView开始数起

                // 监听滚动事件
                popItemExListView.setOnScrollListener(new AbsListView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {

                    }

                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                        //Log.i(TAG,"firstVisibleItem=" + firstVisibleItem + " visibleItemCount=" + visibleItemCount + " totalItemCount=" + totalItemCount);
                        if((firstVisibleItem == 0 && visibleItemCount == totalItemCount) || firstVisibleItem > 0 && firstVisibleItem + visibleItemCount == totalItemCount){
                            popItemShowMore.setVisibility(View.GONE);
                        }else{
                            popItemShowMore.setVisibility(View.VISIBLE);
                        }
                    }
                });
            }
        }
    }

    @Override
    public void onDestroy() {
        Log.i("jishi", "关闭技师弹窗");
        mFloatLayout.setVisibility(View.GONE);
        ProcessWaitingUtil.closeProcessDialog();
        super.onDestroy();
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    Log.i(TAG,"接口返回：" + msg.obj.toString());
                    String resultString = msg.obj.toString();
                    if(msg.arg1 == GET_HALL_ITEM){
                        if(resultString.startsWith("Msg") || TextUtils.isEmpty(resultString)){
                            Log.i(TAG,"没有查询到大堂做钟类型信息");
                        }else{
                            hallMassage = XmlParseUtils.getInstance().parseMassageItem(resultString);
                            // 根据配置隐藏不需要显示的做钟类型
                            if(!TextUtils.isEmpty(pluginBean.getServeDel())){
                                try{
                                    List<Massage> tempMassage = new ArrayList<>();
                                    String[] serverDel = pluginBean.getServeDel().split(",");
                                    for(int i = 0; i < hallMassage.size(); i ++){
                                        Massage massage = hallMassage.get(i);
                                        for(int j = 0;j < serverDel.length;j ++){
                                            if(massage.getMassagename().equals(serverDel[j])){
                                                tempMassage.add(massage);
                                            }
                                        }
                                    }
                                    if(tempMassage.size() > 0){
                                        hallMassage.removeAll(tempMassage);
                                    }
                                }catch (Exception e){

                                }
                            }

                            // 根据配置对项目进行排序
                            if(!TextUtils.isEmpty(pluginBean.getServeSort())){
                                try{
                                    List<Massage> tempMassage = new ArrayList<>();
                                    String[] serveSort = pluginBean.getServeSort().split(",");
                                    for(int i = 0;i < serveSort.length;i ++){
                                        for(int j = 0; j < hallMassage.size(); j ++){
                                            Massage massage = hallMassage.get(j);
                                            if(serveSort[i].equals(massage.getMassagename())){
                                                tempMassage.add(massage);
                                            }
                                        }
                                    }
                                    if(tempMassage.size() > 0){
                                        hallMassage.removeAll(tempMassage);
                                        hallMassage.addAll(0,tempMassage);
                                    }
                                }catch (Exception e){

                                }
                            }

                            // 将大堂项目加入到总的项目中
                            if(hallMassage.size() > 0 && isShowHallItem == true){
                                massageNamesChild.add(hallMassage);
                            }

                            // 如果都为itemClass1和itemClass2的配置都为false的时，则默认显示大堂和全身
                            if(isShowNoHallItem == false && isShowHallItem == false){
                                massageNamesChild.add(hallMassage);
                            }
                        }
                        getNoHallItem();
                    }else if(msg.arg1 == GET_NO_HALL_ITEM){
                        if(resultString.startsWith("Msg") || TextUtils.isEmpty(resultString)){
                            Log.i(TAG,"没有查询到全身做钟类型信息");
                        }else{
                            noHallMassage = XmlParseUtils.getInstance().parseMassageItem(resultString);
                            // 根据配置隐藏不需要显示的做钟类型
                            if(!TextUtils.isEmpty(pluginBean.getServeDel())){
                                try{
                                    List<Massage> tempMassage = new ArrayList<>();
                                    String[] serverDel = pluginBean.getServeDel().split(",");
                                    for(int i = 0; i < noHallMassage.size(); i ++){
                                        Massage massage = noHallMassage.get(i);
                                        for(int j = 0;j < serverDel.length;j ++){
                                            if(massage.getMassagename().equals(serverDel[j])){
                                                tempMassage.add(massage);
                                            }
                                        }
                                    }
                                    if(tempMassage.size() > 0){
                                        noHallMassage.removeAll(tempMassage);
                                    }
                                }catch (Exception e){

                                }
                            }

                            // 根据配置对项目进行排序
                            if(!TextUtils.isEmpty(pluginBean.getServeSort())){
                                try{
                                    List<Massage> tempMassage = new ArrayList<>();
                                    String[] serveSort = pluginBean.getServeSort().split(",");
                                    for(int i = 0;i < serveSort.length;i ++){
                                        for(int j = 0; j < noHallMassage.size(); j ++){
                                            Massage massage = noHallMassage.get(j);
                                            if(serveSort[i].equals(massage.getMassagename())){
                                                tempMassage.add(massage);
                                            }
                                        }
                                    }
                                    if(tempMassage.size() > 0){
                                        noHallMassage.removeAll(tempMassage);
                                        noHallMassage.addAll(0,tempMassage);
                                    }
                                }catch (Exception e){

                                }
                            }

                            if(noHallMassage.size() > 0 && isShowNoHallItem == true){
                                massageNamesChild.add(noHallMassage);
                            }

                            // 如果都为itemClass1和itemClass2的配置都为false的时，则默认显示大堂和全身
                            if(isShowNoHallItem == false && isShowHallItem == false){
                                massageNamesChild.add(noHallMassage);
                            }
                        }
                        ProcessWaitingUtil.closeProcessDialog();
                        setMassageAdapter();
                    }else if(msg.arg1 == GET_THERAPY_DATA){
                        if(resultString.startsWith("Msg") || TextUtils.isEmpty(resultString)){
                            Log.i(TAG,"没有查询到技师资料!");
                            if(TextUtils.isEmpty(resultString)){
                                CustomToast customToast = CustomToast.makeText(context,
                                        getResources().getString(R.string.connect_err),Toast.LENGTH_SHORT,false);
                                customToast.setGravity(Gravity.CENTER,0,0);
                                customToast.show();
                                if(therapies != null){
                                    therapies.clear();
                                }
                                if(adapter != null){
                                    adapter.notifyDataSetChanged();
                                }
                                connectErrorTips.setVisibility(View.VISIBLE);
                            }else{
                                if(therapies != null){
                                    therapies.clear();
                                }
                                if(adapter != null){
                                    adapter.notifyDataSetChanged();
                                }
                                noStatusTips.setVisibility(View.VISIBLE);
                            }
                        }else{
                            therapies = XmlParseUtils.getInstance().parseTherapy(resultString);

                            // 筛选出不是空闲和当值的技师
                            if(status.equals("空闲")){
                                List<Therapy> tempTherapy = new ArrayList<>();
                                for(int i = 0; i < therapies.size(); i ++){
                                    Therapy therapy = therapies.get(i);
                                    if(therapy.getOnduty().equals("未当值") || therapy.getOnduty().equals("当值") && therapy.getStatus().equals("倒牌")){
                                        tempTherapy.add(therapy);
                                    }
                                }
                                therapies.removeAll(tempTherapy);
                            }

                            // 根据配置是否对技师进行排序
                            if(pluginBean != null){
                                if(pluginBean.isArtNumSort()){
                                    try {
                                        Collections.sort(therapies,new SortByArtNo());
                                    }catch (Exception e){

                                    }
                                }
                            }

                            if(sex.equals("女")){// 筛选出女技师
                                List<Therapy> tempTherapy = new ArrayList<>();
                                for(int i = 0;i < therapies.size();i ++){
                                    Therapy therapy = therapies.get(i);
                                    if(!TextUtils.isEmpty(therapy.getSex())){
                                        if(therapy.getSex().equals("男")){
                                            tempTherapy.add(therapy);
                                        }
                                    }
                                }
                                therapies.removeAll(tempTherapy);
                            }else if(sex.equals("男")){// 筛选出男技师
                                List<Therapy> tempTherapy = new ArrayList<>();
                                for(int i = 0;i < therapies.size();i ++){
                                    Therapy therapy = therapies.get(i);
                                    if(!TextUtils.isEmpty(therapy.getSex())){
                                        if(therapy.getSex().equals("女")){
                                            tempTherapy.add(therapy);
                                        }
                                    }
                                }
                                therapies.removeAll(tempTherapy);
                            }
                            if(therapies.size() == 0){
                                noStatusTips.setVisibility(View.VISIBLE);
                            }else{
                                noStatusTips.setVisibility(View.GONE);
                            }

                            adapter = new PopTherapListAdapter(context, therapies,massage.getIshall());
                            popTherapyGridView.setAdapter(adapter);
                            popTherapyGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Therapy therapy = therapies.get(position);
                                    getTherapyDataByArtNo(therapy.getArtNo());
                                }
                            });
                        }
                        ProcessWaitingUtil.closeProcessDialog();
                    }else if(msg.arg1 == GET_THERAPY_DATA_BY_ARTNO){
                        if(resultString.startsWith("Msg") || TextUtils.isEmpty(resultString)){
                            CustomToast customToast = CustomToast.makeText(context,getResources().getString(R.string.connect_err),Toast.LENGTH_SHORT,false);
                            customToast.setGravity(Gravity.CENTER,0,0);
                            customToast.show();
                        }else{
                            therapyList = XmlParseUtils.getInstance().parseTherapy(resultString);
                            if(therapyList.size() > 0){
                                setDialog();
                            }else{
                                CustomToast customToast = CustomToast.makeText(context,getResources().getString(R.string.connect_err),Toast.LENGTH_SHORT,false);
                                customToast.setGravity(Gravity.CENTER,0,0);
                                customToast.show();
                            }
                        }
                        ProcessWaitingUtil.closeProcessDialog();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    public void setDialog(){
        if(AvoidDoubleClickedUtils.isNotDoubleClicked()){
            return;
        }
        if(detailsDialog == null){
            detailsDialog = new JxdJSPopupTherapyDetailsDialog(this, R.style.introDialog);
            detailsDialog.setCanceledOnTouchOutside(true);
            detailsDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
        }else{
            detailsDialog = null;
            detailsDialog = new JxdJSPopupTherapyDetailsDialog(this, R.style.introDialog);
            detailsDialog.setCanceledOnTouchOutside(true);
            detailsDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
        }
        Log.i(TAG,"isHall=" + massage.getIshall());
        detailsDialog.setTherapies(therapyList);
        detailsDialog.setIsHall(massage.getIshall());
        detailsDialog.setPluginBean(pluginBean);
        detailsDialog.setMachineBean(machineBean);
        detailsDialog.setArrangeTimeOut(pluginBean.getArrangeTimeOut());
        EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_JISHI + "\t" +
                MyApplication.interfaceVersion + "\t" + "选技师"));
        detailsDialog.show();
    }
}
